#!/usr/bin/env bash
set -e
if [[ -z "$1" ]]; then
  echo 'Usage '$0' [path-to-project]'
  exit 1
fi

if [[ ! -d "$1" ]]; then
  echo "Path '$1' does not exist or its not a directory"
  exit 1
fi

ADDON_PATH=$(cd `dirname $0` && pwd)
cp -r $ADDON_PATH/skel/* $1/

sed  -i.bak '/^  env\-storage\:[^$]*$/a\
  ardb-storage:'$'\n' "$1/docker/containers/compose.yml" && rm -f "$_.bak"

revision='0.10-a'
echo 'ardb[docker/containers/ardb]: -t ${COMPOSE_PROJECT_NAME}/ardb:'$revision >> "$1/docker/Buildfile"

echo "
ARDB_HOST=127.0.0.1
ARDB_PORT=2112

export ARDB_HOST \\
  ARDB_PORT \\
  ARDB_BIND=\"\${ARDB_HOST}:\${ARDB_PORT}\"

" >> "$1/docker/env.sh"

if [[ ! -f "$1/docker/env.dev.sh" ]]; then
  echo '#!/usr/bin/env bash' > "$1/docker/env.dev.sh"
fi

echo "
ARDB_HOST=ardb
export ARDB_HOST \\
  ARDB_BIND=\"0.0.0.0:\${ARDB_PORT}\"

" >> "$1/docker/env.dev.sh"

sed  -i.bak '/^environment\:[^$]*$/a\
\ \ \-\ ARDB_PORT=\$ARDB_PORT'$'\n' "$1/docker/containers/php/container.yml" && rm -f "$_.bak"

sed  -i.bak '/^environment\:[^$]*$/a\
\ \ \-\ ARDB_HOST=\$ARDB_HOST'$'\n' "$1/docker/containers/php/container.yml" && rm -f "$_.bak"
